﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using TS.Entities;
using TS.DataAccess;
using System.Data.SqlClient;

namespace TS.Bussiness
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class OwnerBussiness
    {

        /// <summary>
        /// Select Owner pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<Owner> ReadOwnerPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
        {
            totalRecord = 0;
            var dataAccess = new OwnerDataAccess();
            try
            {
                if (pageIndex >= 0 && pageSize > 0)
                {
                    var result = dataAccess.ReadOwnerPaged(pageSize, pageIndex, strFilter, strOrderBy, out totalRecord);
                    if (result != null)
                    {
                        return result;
                    }
                    return null;
                }
                return null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        /// <summary>
        /// Insert Owner
        /// </summary>
        /// <param name="Owner"></param>
        /// <returns></returns>
        public int InsertOwner(Owner owner)
        {
            if (string.IsNullOrEmpty(owner.Name)) return -1;
            OwnerDataAccess ownerDao = new OwnerDataAccess();
            try
            {
                return ownerDao.InsertOwner(owner);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        /// <summary>
        /// Get All Owner
        /// </summary>
        /// <returns></returns>
        public Collection<Owner> GetAllOwner()
        {
            OwnerDataAccess grpDao = new OwnerDataAccess();
            try
            {
                Collection<Owner> grlList = new Collection<Owner>();
                grlList = grpDao.GetAllOwner();
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public Owner GetOwnerById(int OwnerId)
        {
            var grpDao = new OwnerDataAccess();
            try
            {
                var grlList = new Owner();
                grlList = grpDao.GetByOwnerId(OwnerId);
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public int UpdateOwner(Owner owner)
        {
            if (string.IsNullOrEmpty(owner.Name)) return -1;
            var ownerDao = new OwnerDataAccess();
            try
            {
                return ownerDao.UpdateOwner(owner);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public int DeleteOwner(Owner owner)
        {
            if (owner.OwnerId <= 0) return -1;
            var ownerDao = new OwnerDataAccess();
            try
            {
                return ownerDao.DeleteOwner(owner);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }
    }
}
